//
//  DFProductsDetailViewController.m
//  cf
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFProductsDetailViewController.h"
#import "DFProductsProductMoel.h"
#import "DFProductsProductCell.h"
#import "DFProductsProductDetailController.h"
#import "PKRequestManager.h"
#import "MJRefresh.h"
@interface DFProductsDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,assign)int page;

@end

@implementation DFProductsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataBaseArr = [NSMutableArray array];
    [self requestDataWithType:0];
    self.tabBarController.tabBar.hidden = YES;
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.backBtn];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.dataBaseArr = [NSMutableArray array];
    self.mainTableView.rowHeight = 100;
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [[UITableView alloc]initWithFrame:CGRectZero];
    
    self.titleLab.text = _brand;
    
    [self.mainTableView registerClass:[DFProductsProductCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.mainTableView];
//  下拉刷新
    self.mainTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];;
    }];
//  上拉加载更多
    self.mainTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    } ];
}

- (void)requestDataWithType:(NSInteger)type
{
    NSString *urlString;

    if (_differentVa == dfProductsRightTableViewController) {
        if (type == 1) {
            _page++;
        }else{
            _page = 1;
        }
        urlString =[NSString stringWithFormat:@"http://lib3.wap.zol.com.cn/index.php?c=Advanced_List_V1&manuId=%@&noParam=1&num=20&page=%d&priceId=noPrice&subcateId=%@",_manuId,_page,_subcateId];
    }else
    {
        urlString = [[NSString stringWithFormat:@"http://lib3.wap.zol.com.cn/index.php?c=Advanced_List_V1&keyword=%@&noParam=1&num=20&page=1&priceId=noPrice",_keyword] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",urlString);
    }
        [PKRequestManager  requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
            
        if (type == 0) {
             [self.dataBaseArr removeAllObjects];
        }
            
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dict in dic[@"data"]) {
            DFProductsProductMoel *model = [[DFProductsProductMoel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataBaseArr addObject:model];
        }
        [self.mainTableView reloadData];
        [self.mainTableView.header endRefreshing];
        [self.mainTableView.footer endRefreshing];
    }
    error:^(NSError *error) {
    NSLog(@"请求失败%@",error);
    [self.mainTableView.header endRefreshing];
    [self.mainTableView.footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataBaseArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFProductsProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DFProductsProductMoel *model = self.dataBaseArr[indexPath.row];
    [cell configureDataWith:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFProductsProductDetailController *detailController = [[DFProductsProductDetailController alloc]init];
    detailController.proId = [self.dataBaseArr[indexPath.row] ID];
    detailController.subcateId = _subcateId;
    [self.navigationController pushViewController:detailController animated:YES];
}

- (void)backTo
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
